<?php
// 计划任务
class PlangoodsorderAction extends CommonAction {
	//魔术方法
public function __construct(){
		parent::__construct();

	}
  public function index(){
  }
   //自动 确认收货 计划任务
  public function order_delivered_plan(){
        $where['pay_status'] = 2;// 支付状态；0，未付款；1，付款中 ；2，已付款
        $where['shipping_status'] = 1;//商品配送情况，0，未发货； 1，已发货；2，已收货；3，备货中
        $where['order_status'] = 1;//订单状态。0，未确认；1，已确 认；2，已取消；3，无效；4，退货；
        $time=time()-24*3600*7;//发货后7天系统自动确认收货
        //小于 7天之前的 时间
        $where['shipping_time'] = array('elt',$time);//订单状态。0，未确认；1，已确 认；2，已取消；3，无效；4，退货；
        $p = $_REQUEST['p'];
        $pagesize = 10;
        $p = !empty($p) ? $p : 1;

        Log::write($p."确认收货计划任务开始：".date("Y-m-d H:i:s"));
		echo "确认收货计划任务开始：".date("Y-m-d H:i:s");

        $start = ($p - 1) * $pagesize;
        //$field = "order_id,order_sn,order_status,shipping_status,pay_status,goods_amount,order_amount,add_time";
        $field = "*";
        $order_model = M('g_order_info');
        $list = $order_model->where($where)//
        ->field($field)->limit($start, $pagesize)->order('shipping_time asc,order_id asc')->select();
        if($list){
          foreach ($list as $key => $value) {
            $this->order_delivered($value);
          }
          //sleep(秒)  usleep(毫秒)  让它睡上一会。
          sleep(2);
          $_REQUEST['p']=$p+1;
          $this->order_delivered_plan();
        }

  }
	  //确认收货
  public function order_delivered($data_info=array()){
      if(empty($data_info)){
        Log::write("计划任务订单不存在：".date("Y-m-d  H:i:s"));
        return false;
      }
      $order_show=$this->get_order_status($data_info);
      if($order_show['code']!='delivered'){//已发货
        Log::write("计划任务订单状态错误：订单号：".$data_info['order_sn']."，".date("Y-m-d  H:i:s"));
        return false;
      }
      $refund=M('g_order_goods_refund')->where(array('member_id'=>$data_info['user_id'],'order_id'=>$data_info['order_id']))->find();
      if(!empty($refund) && $refund['refund_status']!='10'){//
        Log::write("计划任务订单退货中 不可确认收货：订单号：".$data_info['order_sn']."，".date("Y-m-d  H:i:s"));
        return false;
      }
      $model_order = D('Mallorder');
      //查询商家信息
      $model_order->startTrans();
      $o_where["order_id"] =  array("eq",$data_info["order_id"]);
      //更改订单状态
      $data = array();
      $data['shipping_status']  = 2;
      $data['finnshed_time']  = time();
      $order_res = $model_order->editOrder($data,$o_where);
            //记录商家日志
      if($order_res!==false){
        //记录订单日志
        $data = array();
        $data['order_id'] = $data_info['order_id'];
        $data['log_role'] = 'system';//系统
        $data['log_msg'] = '系统自动确认收货';
        $data['log_orderstate'] = '40';
        $insert = $model_order->addOrderLog($data);

        ///用户返利 返积分 认证等
        $this->set_member_exchange($data_info);///用户 返利等操作
        $model_order->commit();
        echo "计划任务订单自动确认收货成功：订单号：".$data_info['order_sn']."，".date("Y-m-d  H:i:s").'<br/>';
        Log::write("计划任务订单自动确认收货成功：订单号：".$data_info['order_sn']."，".date("Y-m-d  H:i:s"));
        return true;
      }else{
        Log::write("计划任务订单自动确认收货失败：订单号：".$data_info['order_sn']."，".date("Y-m-d  H:i:s"));
        return false;
      }
  }


   ///用户返利 返积分 认证等   update hkj 取消 消费大于 188  认证
    ///uodate hkj 如果商品设置佣金  123 级返利不设置
    public function set_member_exchange($data_info = array())
    {
        if (empty($data_info)) {
            return false;
        }
        $pre = C('DB_PREFIX');//表前缀
        //用户自己返利  用户积分增加  返利日志 积分增加日志
        $field = 'mem.member_card,mem.recommend_code,mem.indirect_recommend_code,mem.indirect2_recommend_code'
            . ',mem.member_status,mem.member_vip_type,mem.member_vip_order'
            . ',md.provinceid,md.cityid,md.areaid';
        $member_info = M()->table($pre . 'member mem')->
        join($pre . 'member_detail md on mem.id=md.member_id')//
        ->where(array('mem.id' => $data_info['user_id']))//
        ->field($field)->find();
        //shop_rebate_status  123 级返利状态 1 返利  0不返
        $shop_rebate_status = 0;//返利类型 1分享返利 0正常返利(123级返利) 2升级返利
        $member_tuijian_data = array();//返利记录

        $order_save_data = array();//订单返利 数据
        $order_save_data['profit_type'] = 0;//返利类型 1分享返利 0正常返利(123级返利)
        $order_save_data['order_profit'] = $data_info['order_amount'];//订单利润= 应付款金额-订单返利
        $order_save_data['order_rebate'] = 0;//订单返利金额

        //直接推荐人
        if ($member_info['recommend_code']) {
            $member_info_1 = M('member')->where(array('member_card' => $member_info['recommend_code']))//
            ->field('id,member_card,member_status,mobile,member_vip_type')->find();

        }
        //间接推荐人
        if ($member_info['indirect_recommend_code']) {
            $member_info_2 = M('member')->where(array('member_card' => $member_info['indirect_recommend_code']))//
            ->field('id,member_card,member_status,mobile,member_vip_type')->find();
        }
        //间接二级推荐人
        if ($member_info['indirect2_recommend_code']) {
            $member_info_3 = M('member')->where(array('member_card' => $member_info['indirect2_recommend_code']))//
            ->field('id,member_card,member_status,mobile,member_vip_type')->find();
        }

        //推荐人中最近的旗舰店
        $flageship_info=getflageship($member_info['recommend_code']);
        $flage_level_info=M('member_level')->where('levelId=3')->find();
        //区域代理推荐人
/*        if ($member_info['areaid']) {
            //用户自己返利  用户积分增加  返利日志 积分增加日志
            $field = 'mem.id,mem.member_status,mem.member_card,mem.mobile,mem.member_vip_type,age.rate';
            $member_info_area = M()->table($pre . 'regional_agent age')->
            join($pre . 'member mem on mem.id=age.member_id')//
            ->where(array('age.areaid' => $member_info['areaid'],'age.status' => 1))//
            ->field($field)->find();
        }*/
        //升级返利 start
        //普通会员 首次 购买 升级vip商品
        if ($data_info['is_upgrade']==1 ) {

            $shop_rebate_status = 2;
            $level_order_info=M('member_level_order')->where('id='.$data_info['order_sn'])->find();//升級訂單信息
            $upgrade_info=M('member_level')->where('levelId='.$level_order_info['levelId'])->find();//等級信息
            $member_vip_order = M('member')->where(array('id' => $data_info['user_id']))->save(array('member_vip_order' => 1,'member_vip_type'=>$level_order_info['levelId']));//
            if (!empty($member_info_1) && !empty($member_info_1['member_vip_type']) && $member_vip_order !== false) {
                //$direct_auth_rebate = M('sys_param')->where(array('param_code' => 'direct_auth_rebate'))->getField('param_value');//返利比例；
                //返利金额
               if ($level_order_info['levelId']==1 &&$level_order_info['last_level']==0){//0升級到1，
                   $direct_auth_rebate=$upgrade_info['award_standard'];
               }elseif ($level_order_info['levelId']==2 &&$level_order_info['last_level']==1){//1升級到2，
                   $direct_auth_rebate=$upgrade_info['award_senior']-$upgrade_info['award_standard'];
               }elseif ($level_order_info['levelId']==2 &&$level_order_info['last_level']==0){//1升級到2，
                   $direct_auth_rebate=$upgrade_info['award_senior'];
               }
                $direct_auth_rebate=10000;
                //direct_auth_rebate返利金额
                if ($direct_auth_rebate > 0) {
                    $order_save_data['order_rebate'] += $direct_auth_rebate;//订单返利金额
                    $log = array();
                    $log['type'] = 5;//消费类型 1订单消费 2充值 3提现   4退款 5 收益 6认证消费
                    $log['type5_type']=1;//收益类型  1直接推荐人 2间接推荐人 3 间接二级推荐人 4代理区域会员
                    $log['order_id'] = $data_info['order_id'];
                    $log['des'] = '推荐人('.$member_info['member_card'].')升级VIP收益￥' . $direct_auth_rebate;
                    //加减用户 记录日志
                    $this->set_member_balance($member_info_1['id'], 1, $direct_auth_rebate, $log);
                    //加减vip 积分
                    $log = array();
                    $log['type'] = 1;////1 推荐vip升级 2 订单消费
                    $log['order_id'] = $data_info['order_id'];
                    $log['des'] = '推荐人('.$member_info['member_card'].')升级VIP';
                    $this->set_member_vip_integral($member_info_1['id'], 1, $direct_auth_rebate, $log);
                    //返利记录
                    $tuijian_data = array();
                    $tuijian_data['member_id'] = $member_info_1['id'];
                    $tuijian_data['order_id'] = $data_info['order_id'];
                    $tuijian_data['type'] = 4;
                    $tuijian_data['money'] = $direct_auth_rebate;
                    $tuijian_data['des'] = '推荐人('.$member_info['member_card'].')升级VIP收益：￥' . $direct_auth_rebate;
                    $member_tuijian_data[] = $tuijian_data;
                }

                /* $log=array();
                 $log['order_id']=$data_info['order_id'];
                 $log['des']='推荐人认证增加：10';
                 //加减用户积分 记录日志 直接推荐人+10积分[认证赠送]
                 $this->set_member_points($member_info_1['id'],1,10,$log);*/
            }


            //
            //旗舰店以外的间接推荐人目前没有返利
            //

            //间接推荐人
/*            if (!empty($member_info_2) && !empty($member_info_2['member_vip_type']) && $member_vip_order !== false) {
                $direct_auth_rebate_one = M('sys_param')->where(array('param_code' => 'direct_auth_rebate_one'))->getField('param_value');//返利比例；
                //direct_auth_rebate返利金额
                if ($direct_auth_rebate_one > 0) {
                    $order_save_data['order_rebate'] += $direct_auth_rebate_one;//订单返利金额
                    $log = array();
                    $log['type'] = 5;//消费类型 1订单消费 2充值 3提现   4退款 5 收益 6认证消费
                    $log['type5_type']=2;//收益类型  1直接推荐人 2间接推荐人 3 间接二级推荐人 4代理区域会员
                    $log['order_id'] = $data_info['order_id'];
                    $log['des'] = '间接推荐人('.$member_info['member_card'].')升级VIP收益￥' . $direct_auth_rebate_one;
                    //加减用户 记录日志
                    $this->set_member_balance($member_info_2['id'], 1, $direct_auth_rebate_one, $log);
                    //返利记录
                    $tuijian_data = array();
                    $tuijian_data['member_id'] = $member_info_2['id'];
                    $tuijian_data['order_id'] = $data_info['order_id'];
                    $tuijian_data['type'] = 4;
                    $tuijian_data['money'] = $direct_auth_rebate_one;
                    $tuijian_data['des'] = '间接推荐人('.$member_info['member_card'].')升级VIP收益：￥' . $direct_auth_rebate_one;
                    $member_tuijian_data[] = $tuijian_data;
                }*/
                /* $log=array();
                 $log['order_id']=$data_info['order_id'];
                 $log['des']='推荐人认证增加：10';
                 //加减用户积分 记录日志 直接推荐人+10积分[认证赠送]
                 $this->set_member_points($member_info_1['id'],1,10,$log);*/
            }
            //间接二级推荐人
/*            if (!empty($member_info_3) && !empty($member_info_3['member_vip_type']) && $member_vip_order !== false) {
                $direct_auth_rebate_two = M('sys_param')->where(array('param_code' => 'direct_auth_rebate_two'))->getField('param_value');//返利比例；
                //direct_auth_rebate返利金额
                if ($direct_auth_rebate_two > 0) {
                    $order_save_data['order_rebate'] += $direct_auth_rebate_two;//订单返利金额
                    $log = array();
                    $log['type'] = 5;//消费类型 1订单消费 2充值 3提现   4退款 5 收益 6认证消费
                    $log['type5_type']=3;//收益类型  1直接推荐人 2间接推荐人 3 间接二级推荐人 4代理区域会员
                    $log['order_id'] = $data_info['order_id'];
                    $log['des'] = '间接二级推荐人('.$member_info['member_card'].')升级VIP收益￥' . $direct_auth_rebate_two;
                    //加减用户 记录日志
                    $this->set_member_balance($member_info_3['id'], 1, $direct_auth_rebate_two, $log);
                    //返利记录
                    $tuijian_data = array();
                    $tuijian_data['member_id'] = $member_info_3['id'];
                    $tuijian_data['order_id'] = $data_info['order_id'];
                    $tuijian_data['type'] = 4;
                    $tuijian_data['money'] = $direct_auth_rebate_two;
                    $tuijian_data['des'] = '间接二级推荐人('.$member_info['member_card'].')升级VIP收益：￥' . $direct_auth_rebate_two;
                    $member_tuijian_data[] = $tuijian_data;
                }
              $log=array();
                 $log['order_id']=$data_info['order_id'];
                 $log['des']='推荐人认证增加：10';
                 //加减用户积分 记录日志 直接推荐人+10积分[认证赠送]
                 $this->set_member_points($member_info_1['id'],1,10,$log);
            }*/
            //区域代理
/*            if (!empty($member_info_area) && $member_vip_order !== false) {
                $direct_auth_rebate_area = M('sys_param')->where(array('param_code' => 'direct_auth_rebate_area'))->getField('param_value');//返利比例；
                //direct_auth_rebate返利金额
                if ($direct_auth_rebate_area > 0) {
                    $order_save_data['order_rebate'] += $direct_auth_rebate_area;//订单返利金额

                    $log = array();
                    $log['type'] = 5;//消费类型 1订单消费 2充值 3提现   4退款 5 收益 6认证消费
                    $log['type5_type']=4;//收益类型  1直接推荐人 2间接推荐人 3 间接二级推荐人 4代理区域会员
                    $log['order_id'] = $data_info['order_id'];
                    $log['des'] = '代理区域会员('.$member_info['member_card'].')升级VIP收益￥' . $direct_auth_rebate_area;
                    //加减用户 记录日志
                    $this->set_member_balance($member_info_area['id'], 1, $direct_auth_rebate_area, $log);
                    //返利记录
                    $tuijian_data = array();
                    $tuijian_data['member_id'] = $member_info_area['id'];
                    $tuijian_data['order_id'] = $data_info['order_id'];
                    $tuijian_data['type'] = 4;
                    $tuijian_data['money'] = $direct_auth_rebate_area;
                    $tuijian_data['des'] = '代理区域会员('.$member_info['member_card'].')升级VIP收益：￥' . $direct_auth_rebate_area;
                    $member_tuijian_data[] = $tuijian_data;
                }
                /* $log=array();
                 $log['order_id']=$data_info['order_id'];
                 $log['des']='推荐人认证增加：10';
                 //加减用户积分 记录日志 直接推荐人+10积分[认证赠送]
                 $this->set_member_points($member_info_1['id'],1,10,$log);*/
          //  }*/
            //旗舰店收益
          if (!empty($flageship_info)){

                $add_child2=$flage_level_info['addChild2'];
                $order_save_data['order_rebate'] += $add_child2;
              $log = array();
              $log['type'] = 5;//消费类型 1订单消费 2充值 3提现   4退款 5 收益 6认证消费
              $log['type5_type']=4;//收益类型  1直接推荐人 2间接推荐人 3 间接二级推荐人 4代理区域会员
              $log['order_id'] = $data_info['order_id'];
              $log['des'] = '旗舰店('.$member_info['member_card'].')升级VIP收益￥' . $add_child2;
              //加减用户 记录日志
              $this->set_member_balance($flage_level_info['id'], 1, $add_child2, $log);
              //返利记录
              $tuijian_data = array();
              $tuijian_data['member_id'] = $flage_level_info['id'];
              $tuijian_data['order_id'] = $data_info['order_id'];
              $tuijian_data['type'] = 4;
              $tuijian_data['money'] = $add_child2;
              $tuijian_data['des'] = '旗舰店('.$member_info['member_card'].')升级VIP收益：￥' . $add_child2;
              $member_tuijian_data[] = $tuijian_data;
           }


        //升级返利 end

        if (empty($shop_rebate_status)) {
            $order_goods = M('g_order_goods')->where(array('order_id' => $data_info["order_id"]))->select();
            $member_model = M('member');
            $goods_model = M("g_goods");
            if ($order_goods) {
                foreach ($order_goods as $key => $value) {
                    //增加销量
                    $goods_model->where(array("goods_id" => $value['goods_id']))->setInc("goods_salesvolume", $value['goods_number']);

                    if ($value['share_card'] && $value['share_money'] > 0) {
                        $member_share = $member_model->where(array('member_card' => $value['share_card']))//
                        ->field('id,member_status,mobile')->find();
                        //认证返利
                        // if (!empty($member_share) && !empty($member_share['member_status'])) {
                        if (!empty($member_share) && $member_share['mobile']) {///绑定手机
                            $shop_rebate_status = 1;//如果商品 分享返利了 123 级返利不执行
                            $order_save_data['profit_type'] = 1;
                            //$rebate_indirect=M('sys_param')->where(array('param_code'=>'rebate_indirect'))->getField('param_value');//返利比例；
                            //$discount_rebate*$rebate_indirect;//返利金额
                            $share_money = $value['share_money'] * $value['goods_number'];
                            $order_save_data['order_rebate'] += $share_money;//订单返利金额

                            $log = array();
                            $log['type'] = 5;//消费类型 1订单消费 2充值 3提现   4退款 5 收益 6认证消费
                            $log['order_id'] = $value['order_id'];
                            $log['des'] = '分享商品收益：￥' . $share_money;
                            //加减用户 记录日志
                            $this->set_member_balance($member_share['id'], 1, $share_money, $log);
                            //返利记录
                            $tuijian_data = array();
                            $tuijian_data['member_id'] = $member_share['id'];
                            $tuijian_data['order_id'] = $value['order_id'];
                            $tuijian_data['type'] = 6;
                            $tuijian_data['money'] = $value['share_money'];
                            $tuijian_data['des'] = '分享商品收益：￥' . $share_money;
                            $member_tuijian_data[] = $tuijian_data;
                        }
                    }
                }
            }
        }
        /*  if (empty($shop_rebate_status)) {///分享 返利
              $order_save_data['order_profit'] = $order_save_data['order_profit'] - $order_save_data['order_rebate'];
              M('g_order_info')->where(array('order_id' => $data_info['order_id']))->save($order_save_data);
          }*/
        //增加用户积分 1：1
        $integral = round($data_info['order_amount']);
        if ($integral > 0) {
            $log = array();
            $log['order_id'] = $data_info['order_id'];
            $log['des'] = '商城订单消费增加：' . $integral;
            //加减用户 记录日志
            $this->set_member_points($data_info['user_id'], 1, $integral, $log);
        }

        //  $shop_rebate_status = 0;//返利类型 1分享返利 0正常返利(123级返利) 2升级返利
        if($member_info['member_vip_type']>0 || $shop_rebate_status==2){/// 如果自己是会员 或者是升级返利
            //加减vip 积分
            $log = array();
            $log['type'] = 2;////1 推荐vip升级 2 订单消费
            $log['order_id'] = $data_info['order_id'];
            $log['money'] = $data_info['order_amount'];
            $log['des'] = '订单累计消费';
            $this->set_member_vip_integral($data_info['user_id'], 1, $data_info['order_amount'], $log);
        }

        ///商城返利比例
        $shop_rebate = M('sys_param')->where(array('param_code' => 'shop_rebate'))->getField('param_value');//返利比例；
        //总返利金额
        $discount_rebate = $data_info['order_amount'] * $shop_rebate;//返利金额
        $order_profit_all=0;
        //123 返利
        if ($shop_rebate_status == 0 && $shop_rebate > 0 && $shop_rebate < 1) {//
            $order_profit_all=$discount_rebate;
            //消费者本身返利比例
            $rebate_consumer = M('sys_param')->where(array('param_code' => 'rebate_consumer'))->getField('param_value');//返利比例；
            ///用户自己返利
            $discount_rebate_my = $discount_rebate * $rebate_consumer;//返利金额
/*            if ($discount_rebate_my > 0) {

                $order_save_data['order_rebate'] += $discount_rebate_my;//订单返利金额

                $log = array();
                $log['type'] = 5;//消费类型 1订单消费 2充值 3提现   4退款 5 收益 6认证消费
                $log['order_id'] = $data_info['order_id'];
                $log['des'] = '订单' . $data_info['order_sn'] . '收益：￥' . $discount_rebate_my;
                //加减用户 记录日志
                $this->set_member_balance($data_info['user_id'], 1, $discount_rebate_my, $log);
                //返利记录
                $tuijian_data = array();
                $tuijian_data['member_id'] = $data_info['user_id'];
                $tuijian_data['order_id'] = $data_info['order_id'];
                $tuijian_data['type'] = 1;
                $tuijian_data['money'] = $discount_rebate_my;
                $tuijian_data['des'] = '订单' . $data_info['order_sn'] . '收益：￥' . $discount_rebate_my;
                $member_tuijian_data[] = $tuijian_data;
            }*/
            ///用户自己返利end

            //直接推荐人返利
            //推荐人是 认证的才返利  是vip才返利
            // if (!empty($member_info_1) && !empty($member_info_1['member_status'])) {
            if (!empty($member_info_1) && $member_info_1['mobile'] && !empty($member_info_1['member_vip_type'])) {///绑定手机
                //$rebate_direct = M('sys_param')->where(array('param_code' => 'rebate_direct'))->getField('param_value');//返利比例；
                //$discount_rebate_1 = $discount_rebate * $rebate_direct;//返利金额
                $level_info_1=M('member_level')->where('levelId='.$member_info_1['member_vip_type'])->find();
                $discount_rebate_1=$discount_rebate * $level_info_1['sale_self'];
                if ($discount_rebate_1 > 0) {

                    $order_save_data['order_rebate'] += $discount_rebate_1;//订单返利金额

                    $log = array();
                    $log['type'] = 5;//消费类型 1订单消费 2充值 3提现   4退款 5 收益 6认证消费
                    $log['type5_type']=1;//收益类型  1直接推荐人 2间接推荐人 3 间接二级推荐人 4代理区域会员
                    $log['order_id'] = $data_info['order_id'];
                    $log['des'] = '直接推荐人('.$member_info['member_card'].')消费收益：￥' . $discount_rebate_1;
                    //加减用户 记录日志
                    $this->set_member_balance($member_info_1['id'], 1, $discount_rebate_1, $log);
                    //返利记录
                    $tuijian_data = array();
                    $tuijian_data['member_id'] = $member_info_1['id'];
                    $tuijian_data['order_id'] = $data_info['order_id'];
                    $tuijian_data['type'] = 2;
                    $tuijian_data['money'] = $discount_rebate_1;
                    $tuijian_data['des'] = '直接推荐人('.$member_info['member_card'].')消费收益：￥' . $discount_rebate_1;
                    $member_tuijian_data[] = $tuijian_data;
                }
            }
            //直接推荐人返利end

            //间接人返利
            //推荐人是 认证的才返利  是vip才返利
            // if (!empty($member_info_2) && !empty($member_info_2['member_status'])) {
            if (!empty($member_info_2) && $member_info_2['mobile'] && !empty($member_info_2['member_vip_type'])) {///绑定手机
               // $rebate_indirect = M('sys_param')->where(array('param_code' => 'rebate_indirect'))->getField('param_value');//返利比例；
                //$discount_rebate_2 = $discount_rebate * $rebate_indirect;//返利金额
                $level_info_2=M('member_level')->where('levelId='.$member_info_2['member_vip_type'])->find();
                $discount_rebate_2=$discount_rebate * $level_info_2['sale_self'];
                if ($discount_rebate_2) {

                    $order_save_data['order_rebate'] += $discount_rebate_2;//订单返利金额

                    $log = array();
                    $log['type'] = 5;//消费类型 1订单消费 2充值 3提现   4退款 5 收益 6认证消费
                    $log['type5_type']=2;//收益类型  1直接推荐人 2间接推荐人 3 间接二级推荐人 4代理区域会员
                    $log['order_id'] = $data_info['order_id'];
                    $log['des'] = '间接推荐人('.$member_info['member_card'].')消费收益：￥' . $discount_rebate_2;
                    //加减用户 记录日志
                    $this->set_member_balance($member_info_2['id'], 1, $discount_rebate_2, $log);
                    //返利记录
                    $tuijian_data = array();
                    $tuijian_data['member_id'] = $member_info_2['id'];
                    $tuijian_data['order_id'] = $data_info['order_id'];
                    $tuijian_data['type'] = 3;
                    $tuijian_data['money'] = $discount_rebate_2;
                    $tuijian_data['des'] = '间接推荐人('.$member_info['member_card'].')消费收益：￥' . $discount_rebate_2;
                    $member_tuijian_data[] = $tuijian_data;
                }
            }
            //间接二级返利
            //推荐人是 认证的才返利  是vip才返利
            // if (!empty($member_info_3) && !empty($member_info_3['member_status'])) {
            if (!empty($member_info_3) && $member_info_3['mobile'] && !empty($member_info_3['member_vip_type'])) {///绑定手机
                //$rebate_indirect_two = M('sys_param')->where(array('param_code' => 'rebate_indirect_two'))->getField('param_value');//返利比例；
                //$discount_rebate_3 = $discount_rebate * $rebate_indirect_two;//返利金额
                $level_info_3=M('member_level')->where('levelId='.$member_info_3['member_vip_type'])->find();
                $discount_rebate_3=$discount_rebate * $level_info_3['sale_self'];
                if ($discount_rebate_3 > 0) {

                    $order_save_data['order_rebate'] += $discount_rebate_3;//订单返利金额

                    $log = array();
                    $log['type'] = 5;//消费类型 1订单消费 2充值 3提现   4退款 5 收益 6认证消费
                    $log['type5_type']=3;//收益类型  1直接推荐人 2间接推荐人 3 间接二级推荐人 4代理区域会员
                    $log['order_id'] = $data_info['order_id'];
                    $log['des'] = '间接二级推荐人('.$member_info['member_card'].')消费收益：￥' . $discount_rebate_3;
                    //加减用户 记录日志
                    $this->set_member_balance($member_info_3['id'], 1, $discount_rebate_3, $log);
                    //返利记录
                    $tuijian_data = array();
                    $tuijian_data['member_id'] = $member_info_3['id'];
                    $tuijian_data['order_id'] = $data_info['order_id'];
                    $tuijian_data['type'] = 5;
                    $tuijian_data['money'] = $discount_rebate_3;
                    $tuijian_data['des'] = '间接二级推荐人('.$member_info['member_card'].')消费收益：￥' . $discount_rebate_3;
                    $member_tuijian_data[] = $tuijian_data;
                }
            }
            //间接二级返利end

            //区域代理返利
            //区域代理是 认证的才返利  是vip才返利
            // if (!empty($member_info_3) && !empty($member_info_3['member_status'])) {
/*            if (!empty($member_info_area)) {///
                // $rebate_indirect_two = M('sys_param')->where(array('param_code' => 'rebate_indirect_two'))->getField('param_value');//返利比例；
                $rebate_indirect_area= $member_info_area['rate'];
                $discount_rebate_area = $discount_rebate * $rebate_indirect_area;//返利金额
                if ($discount_rebate_area > 0) {

                    $order_save_data['order_rebate'] += $discount_rebate_area;//订单返利金额

                    $log = array();
                    $log['type'] = 5;//消费类型 1订单消费 2充值 3提现   4退款 5 收益 6认证消费
                    $log['type5_type']=4;//收益类型  1直接推荐人 2间接推荐人 3 间接二级推荐人 4代理区域会员
                    $log['order_id'] = $data_info['order_id'];
                    $log['des'] = '代理区域会员('.$member_info['member_card'].')消费收益：￥' . $discount_rebate_area;
                    //加减用户 记录日志
                    $this->set_member_balance($member_info_area['id'], 1, $discount_rebate_area, $log);
                    //返利记录
                    $tuijian_data = array();
                    $tuijian_data['member_id'] = $member_info_area['id'];
                    $tuijian_data['order_id'] = $data_info['order_id'];
                    $tuijian_data['type'] = 5;
                    $tuijian_data['money'] = $discount_rebate_area;
                    $tuijian_data['des'] = '代理区域会员('.$member_info['member_card'].')消费收益：￥' . $discount_rebate_area;
                    $member_tuijian_data[] = $tuijian_data;
                }
            }*/
            // //区域代理返利end

        }
        //订单返利类型
        $order_save_data['profit_type'] = $shop_rebate_status;
        $order_save_data['order_profit_all'] = $order_profit_all;
        //订单利润 order_profit
        //订单返利金额 order_rebate
        //订单返利总金额 返利池 order_profit_all
        $order_save_data['order_profit'] = $order_save_data['order_profit'] - $order_save_data['order_rebate'];
        M('g_order_info')->where(array('order_id' => $data_info['order_id']))->save($order_save_data);


        if (!empty($member_tuijian_data)) {//记录手游记录
            $rebate_record_model = M('rebate_record');
            $time = time();
            foreach ($member_tuijian_data as $key => $value) {
                $value['add_time'] = $time;
                $value['status'] = 1;//状态 1用户收益 2 商家返利
                $rebate_record_model->add($value);
            }
        }

    }

    //获取订单状态
  public function get_order_status($order_info=array()){
      if(empty($order_info)){
        return array();
      }
// pay_status支付状态；0，未付款；1，付款中 ；2，已付款
// shipping_status 商品配送情况，0，未发货； 1，已发货；2，已收货；3，备货中
//order_status订单状态。0，未确认；1，已确 认；2，已取消；3，无效；4，退货；
      $order_status=array('0'=>'未确认','1'=>'已确认','2'=>'已取消','3'=>'无效','4'=>'已退货');
      $shipping_status=array('0'=>'未发货','1'=>'已发货','2'=>'已完成','3'=>'备货中');
      $code=$name="";
      if($order_info['order_status']==1){//已确认
        switch ($order_info['shipping_status']) {
          case '1':
            $code="delivered";//已发货
            break;
          case '2':
            $code="received";//已收货
            break;
          default:
            $code='nondelivery';//未发货
            break;
        }
        return array('code'=>$code,'name'=>$shipping_status[$order_info['shipping_status']]);
      }else{//未确认 
           //2已取消；3无效；4，退货；
          if(in_array($order_info['order_status'], array('2','3','4'))){
                return array('code'=>'finish','name'=>$order_status[$order_info['order_status']]);
          }else{////未确认
            if($order_info['pay_status']==2){//已付款 待确认
                    return array('code'=>'unconfirmed','name'=>'待确认');
            }else{
                    return array('code'=>'nopay','name'=>'未付款');
            }
          }
      }
      
  }



}